package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Override
    public ResponseResult deleteChannelById(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean idDelete = removeById(id);
        if (!idDelete) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findChannelsByName(AdChannelDto dto) {
        Page<WmChannel> wmChannelPage = new Page<>(dto.getPage(), dto.getSize());

        wmChannelPage = page(wmChannelPage, Wrappers.<WmChannel>lambdaQuery().like(WmChannel::getName, dto.getName()));


        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) wmChannelPage.getTotal()); // 创建分页响应结果对象
        responseResult.setData(wmChannelPage.getRecords()); // 设置响应结果的数据为查询结果列表

        return responseResult; // 返回响应结果
    }

    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannel.setCreatedTime(new Date());
        boolean isSave = save(wmChannel);
        System.out.println(isSave);
        if (!isSave) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannelById(AdChannel adChannel) {

        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(adChannel.getDescription())) {

            boolean isUpdate = update(Wrappers.<WmChannel>lambdaUpdate().eq(WmChannel::getId, adChannel.getId()).set(WmChannel::getStatus, adChannel.getStatus()));
            if (!isUpdate) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        WmChannel wmChannel = new WmChannel();
        wmChannel.setCreatedTime(new Date());
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannel.setIsDefault(true);
        boolean isUpdate = update(wmChannel, Wrappers.<WmChannel>lambdaUpdate().eq(WmChannel::getId, wmChannel.getId()));
        if (!isUpdate) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findChannels() {

        return ResponseResult.okResult(list());
    }
}
